package org.scuecsz.view.mbean.adminmnage;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import org.scuecsz.model.bussiness.AdminManageModel;
import org.scuecsz.model.to.DIPLOMA;
import org.scuecsz.model.to.USER_MAIN;
import org.scuecsz.view.utils.ConditionCombin;
import org.scuecsz.view.utils.FaceUtil;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Administrator
 */
public class ManageUserMbean extends AdminMbean{
    private Map queryUsersCondtion;
    private USER_MAIN user;
    private List users;
    private DIPLOMA diploma;
    public ManageUserMbean() {
    }

    public String newSelectUsersAction(){
         queryUsersCondtion=ConditionCombin.combinFirstQueryCondtion(queryUsersCondtion, null);
         System.out.println(queryUsersCondtion);
         selectUsersAction();
         return null;
    }

    public String selectUsersAction() {
        Map rs;
        try {
            String condition=(String) queryUsersCondtion.get(ConditionCombin.QSTRING);
            List values= (List) queryUsersCondtion.get(ConditionCombin.QPARAMS);
            rs = AdminManageModel.queryUsers(users,condition,values, pageNum, size);
            users = (List) rs.get(AdminManageModel.USERLIST);
            maxPage=(Integer)rs.get(AdminManageModel.MAXPAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String query(){
        return null;
    }


    public String nextPageUsersAction() {
        nextPage();
        selectUsersAction();
        return null;
    }

    public String prePageUsersAction() {
        prePage();
        selectUsersAction();
        return null;
    }
    public void changeStatus() {
        String status=FaceUtil.getPageRequestParameterValue("status");
        String userid= FaceUtil.getPageRequestParameterValue("usermainid");
        try {
            AdminManageModel.changeStatus(status, userid);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        selectUsersAction();
    }

    public String selectUserAction() {
        String id =  FaceUtil.getPageRequestParameterValue("usermainid");
        USER_MAIN usertemp=null;
           for(int i=0;i<users.size();i++){
                 usertemp=(USER_MAIN) users.get(i);
                 if(id.equals(usertemp.getID())){
                    user=usertemp;
                   try {
                    diploma=AdminManageModel.getUserDiploma(user.getUSER_ID());
                    FaceUtil.setSessionAttribute("DIPLOMA", diploma);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return "useredit";
                 }
           }
        return null;
    }

    public void paintDIPLOMA(OutputStream stream, Object object) throws IOException{
        DIPLOMA tdiploma=(DIPLOMA) FaceUtil.getSessionAttributeValueThenRemove("DIPLOMA");
        stream.write(tdiploma.getDIPLOMA());
        stream.flush();
        stream.close();
    }

   public String saveUserAction() {
        try {
        AdminManageModel.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List getUsers() {
        if (users == null) {
            newSelectUsersAction();
        }
        return users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    public USER_MAIN getUser() {
        return user;
    }

    public void setUser(USER_MAIN user) {
        this.user = user;
    }

    public DIPLOMA getDiploma() {
        return diploma;
    }

    public void setDiploma(DIPLOMA diploma) {
        this.diploma = diploma;
    }
}
